﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GRTVPluginConsole.Entity;
using System.Net;

namespace GRTVPluginConsole.Listener.Processors
{
    class PluginSpinAroundProcessor
    {
        //Form1 callback;
        GRTVPluginConsole.Listener.PluginListenerThread.DriveEventReceived callback;

        private byte[] data;
        public PluginSpinAroundProcessor(IPEndPoint ep, byte[] data, GRTVPluginConsole.Listener.PluginListenerThread.DriveEventReceived callback)
        {
            this.data = data;
            this.callback = callback;
        }

        public void ThreadLoop(Object threadContext)
        {
            if (callback != null)
            {
                SpinRoundEvent entity = new SpinRoundEvent();
                entity.overtakeET = BitConverter.ToDouble(data, 2);
                entity.slotId = BitConverter.ToInt32(data, 10);
                entity.name = getString(data, 14, 32);
                callback(this, entity);
            }
        }

        private string getString(byte[] data, int offset, int length)
        {
            for (int i = 0; i < length; i++)
            {
                if (data[offset + i] == 0)
                {
                    return Encoding.ASCII.GetString(data, offset, i);
                }
            }
            return Encoding.ASCII.GetString(data, offset, length);
        }
    }
}
